package cn.liuweiwei.service.impl;

import cn.liuweiwei.domain.Message;
import cn.liuweiwei.mapper.MessageMapper;
import cn.liuweiwei.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public PageInfo<Message> findAllMessage(String page, String limit) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<Message> messages = messageMapper.findAll();
        return new PageInfo<>(messages);
    }

    @Override
    public List<Message> findOneMessageByEmail(String email) {
        return messageMapper.findOneMessageByEmail(email);
    }

    @Override
    @Transactional
    public void insertOneMessage(Message message) {
        messageMapper.insertOne(message);
    }


    @Override
    @Transactional
    public void deleteMessageByIds(String[] ids) {
        for (String id : ids) {
            messageMapper.deleteById(id);
        }
    }

}
